package com.ip.student.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ip.common.constant.Result;
import com.ip.common.enums.CodeEnum;
import com.ip.common.utils.StrUtils;
import com.ip.user.models.common.annotation.RequiresPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.ip.student.models.service.IStudentPhysicalService;
import com.ip.student.models.bo.StudentPhysicalSaveBO;
import com.ip.student.models.bo.StudentPhysicalBO;
import com.ip.student.models.dto.StudentPhysicalDTO;

/**
 * 体测信息控制器
 *
 * @author 张景奇
 * @Date 2025-11-04 14:08:57
 */

@RestController
@RequestMapping("/cms/studentPhysical")
@Slf4j
@Api(value = "StudentPhysicalController",tags = "体测信息接口")
public class StudentPhysicalController {

    @DubboReference
    private IStudentPhysicalService studentPhysicalService;

    @ApiOperation(value = "添加体测信息",notes = "添加体测信息")
    @PostMapping(value = "/insert")
    @RequiresPermission("student:physical:add")
    public Result insert(@RequestBody StudentPhysicalSaveBO saveBO) {
        boolean flag = studentPhysicalService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存体测信息",notes = "保存体测信息")
    @RequiresPermission("student:physical:add")
    public Result save(@RequestBody StudentPhysicalSaveBO saveBO){
        if(saveBO.getPhysicalId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改体测信息",notes = "修改体测信息")
    @PostMapping("/update")
    @RequiresPermission("student:physical:update")
    public Result update(@RequestBody StudentPhysicalSaveBO saveBO){
        StudentPhysicalDTO dto = new StudentPhysicalDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setPhysicalId(saveBO.getPhysicalId());
        boolean flag = studentPhysicalService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除体测信息",notes = "删除体测信息")
    @PostMapping("/delete")
    @RequiresPermission("student:physical:delete")
    public Result delete(@RequestBody Map<String, Object> params){
        Object physicalIdObj = params.get("physicalId");
        if (physicalIdObj == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        Integer physicalId = Integer.valueOf(physicalIdObj.toString());
        boolean flag = studentPhysicalService.deleteById(physicalId);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除体测信息",notes = "批量删除体测信息")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    @RequiresPermission("student:physical:delete")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        studentPhysicalService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    @RequiresPermission("student:physical:query")
    public Result<StudentPhysicalDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        StudentPhysicalDTO dto = studentPhysicalService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "体测信息列表",notes = "体测信息列表")
    @PostMapping("/queryPageList")
    @RequiresPermission("student:physical:query")
    public Result<Page<StudentPhysicalDTO>> queryPageList(@RequestBody StudentPhysicalBO bo){
        Wrapper<StudentPhysicalDTO> wrapper = new EntityWrapper<>();
        
        // 根据学生ID进行查询
        if (bo.getStudentId() != null) {
            wrapper.eq("student_id", bo.getStudentId());
        }
        
        // 根据学年进行模糊查询
        if (StrUtils.isNotEmpty(bo.getSchoolYear())) {
            wrapper.like("school_year", bo.getSchoolYear());
        }
        
        // 根据学期进行模糊查询
        if (StrUtils.isNotEmpty(bo.getSemester())) {
            wrapper.like("semester", bo.getSemester());
        }

        Page<StudentPhysicalDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = studentPhysicalService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}